package com.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class BaseDao {
    static SqlSessionFactory sqlMapper ;
    static {
        String resources = "applicationContext.xml";
        Reader reader=null;
        try {
            reader= Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlMapper= new SqlSessionFactoryBuilder().build(reader);
    }

    public static SqlSession getSession(){
        SqlSession session=sqlMapper.openSession();
        return session;
    }

    public static void main(String[] args) {
        SqlSession session=  BaseDao.getSession();
        System.out.println(session);
    }
    public static void closeSession(SqlSession session){
        session.close();
    }
}